package br.edu.ufcg.p1.barzin.async;

import static br.edu.ufcg.p1.barzin.util.PrefManConstants.TABLET_ID;
import static br.edu.ufcg.p1.barzin.util.PrefManConstants.TEM_CONTA;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import br.edu.ufcg.p1.barzin.App;
import br.edu.ufcg.p1.barzin.event.ContaFechadaEvento;
import br.edu.ufcg.p1.barzin.event.EventBus;
import br.edu.ufcg.p1.barzin.util.HTTPCalls;

public class ContaAbertaTask extends AsyncTask<Integer, Integer, Boolean> {

	@Override
	protected Boolean doInBackground(Integer... versaoCardapio) {
		Integer idTablet = App.environment().getSharedPreferences().getInt(TABLET_ID, -1);

		boolean aberta = HTTPCalls.checaContaAberta(idTablet);
		if (!aberta) {
			return App.environment().getDao().limpaDados();
		}
		
		return false;
	}

	@Override
	protected void onPostExecute(Boolean result) {
		if (result) {
			SharedPreferences.Editor editor = App.environment().getSharedPreferences().edit();
			editor.putInt(TEM_CONTA, 0);
			editor.commit();
			EventBus.getInstance().publica(new ContaFechadaEvento());
		}
	}
	
}
